#!/bin/bash
function install_php() {
    echo "安装PHP" >>$logs_file
    rm -rf /tmp/php-comp.log
    phpPid=`pidof php-fpm`
    if [ -n "$phpPid" ];then
        whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "系统中已经有PHP在运行，请先卸载！！！" 10 70
        sleep 0.15
        return 1
    fi
    
    phpHome="$workHome/packages/php"
    cd $phpHome
    phpVer=$(whiptail --backtitle "Author: Elvis" --title "PHP安装配置" --inputbox \
        "输入需要安装的PHP版本，如果本地不存在，则需要联网下载\n推荐版本为最新7版本或5.6.40、5.5.38" 12 60 7.4.5 3>&1 1>&2 2>&3)
    if [ "$?" != "0" ];then
        sleep 0.20
        return 1
    fi
    
    if [ -z "$phpVer" ];then
        phpVer="7.4.5"
    fi
    phpVer=`echo $phpVer`
    if [ ! -f $phpHome/php-$phpVer.tar.bz2 -a "$die_network" == "ture" ];then
        whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "系统未联网，且本地不存在需要安装的PHP版本！！！" 10 60
        sleep 0.15
        return 1
    fi
    
    mainPhpVer=`echo $phpVer|awk -F"." '{print $1}'`
    subPhpVer=`echo $phpVer|awk -F"." '{print $2}'`
    
    phpPath=$(whiptail --backtitle "Author: Elvis" --title "PHP安装配置" --inputbox \
        "输入PHP安装位置" 10 60 "/usr/local/php$mainPhpVer" 3>&1 1>&2 2>&3)
    if [ "$?" != "0" ];then
        sleep 0.20
        return 1
    fi
    phpPath=${phpPath:-/usr/local/php$mainPhpVer}
    phpPath=`echo $phpPath`
    phpPath=${phpPath%/}
    isNull=`ls $phpPath/* 2>/dev/null | wc -l`
    if [[ $isNull -gt 0 ]];then
        whiptail --backtitle "Author: Elvis" --title "警告" --yes-button "继续" --no-button "退出" --yesno "给出的PHP安装目录非空，继续安装会删除已有数据！\n\n是否继续安装？" 10 60
        if [ "$?" != "0" ];then
         sleep 0.10
         return 1     
        fi
    fi
  
    AapacheSupport=""
    whiptail --backtitle "Author: Elvis" --title "提示" --yesno "是否为Apache添加PHP支持？\n注意：\n需提前安装Apache\n安装完成后，参看说明修改Apache配置以启用PHP支持。" 10 60
    if [ "$?" == "0" ];then
        apxsPath=$(whiptail --backtitle "Author: Elvis" --title "Apache安装路径" --inputbox "输入Apache apxs文件位置，一般位于apache安装目录
        bin文件夹中" 10 60 "/usr/local/apache2/bin/apxs" 3>&1 1>&2 2>&3)
        if [ "$?" != "0" ];then
            sleep 0.20
            return 1
        else
           apxsPath=`echo $apxsPath`
           if [ ! -f  $apxsPath ];then
               whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "指定位置未找到apxs" 10 60
               sleep 0.15
               return 1
           else
               AapacheSupport="--with-apxs2=$apxsPath"
               apachePath=${apxsPath%/bin*}
           fi
        fi
    fi    
    
    {
    echo 1
    if [ "$die_network" == "true" ];then
        ping -c 2 -w 5 180.76.76.76 >/dev/null 2>&1
        if [ "$?" != "0" ];then
           die_network="true"
        else
           die_network="false"
        fi
    else
        sleep 1
    fi
    if [[ ! -f $phpHome/php-$phpVer.tar.bz2 && "$die_network" == "false" ]];then
        msg="下载PHP安装包"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n10\n$msg\nXXX"
        echo 10
        echo "首先使用curl下载，curl失败后尝试使用wget下载" >>$logs_file
        echo -e "XXX\n15\n下载PHP安装包中......\nXXX"
        echo 15
        curl -O https://www.php.net/distributions/php-$phpVer.tar.bz2 >>$logs_file 2>&1
        tar jxf php-$phpVer.tar.bz2 -C /tmp/>/dev/null 2>&1
        if [ "$?" != "0" ];then
            rm -rf /tmp/php-${phpVer}
            rm -rf php-${phpVer}.tar.bz2
            sleep 1
            echo "">>$logs_file
            wget -t5 https://www.php.net/distributions/php-$phpVer.tar.bz2 >>$logs_file 2>&1
        else
            rm -rf /tmp/php-${phpVer}
        fi
                
        if [ "$?" != "0" ];then
           msg="下载PHP失败，请检查网络或者不存在此版本的PHP！！！"
           echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
           echo -e "XXX\n20\n$msg\nXXX"
           echo 20
           echo "$msg" >/tmp/.php.err.info
           sleep 3
           return 1
        fi
    elif [[ ! -f $phpHome/php-$phpVer.tar.bz2 && "$die_network" == "true" ]];then
        msg="你的主机未联网，请提前手动下载安装包，使用离线安装！！！"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n20\n$msg\nXXX"
        echo 20
        echo "$msg" >/tmp/.php.err.info
        sleep 3
        return 1
    fi
    
    
    msg="安装所需基础RPM包"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n30\n$msg\nXXX"
    echo 30
    if [ "$system_ver" == "6" ];then
          curl -so /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo >>$logs_file 2>&1
    elif [ "$system_ver" == "7" ];then
          curl -so /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo >>$logs_file 2>&1
    elif [ "$system_ver" == "8" ];then
          yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm >>$logs_file 2>&1
          sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
          sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
    fi
    yum -y install glib2 glib2-devel make gcc gcc-c++ cmake curl curl-devel ncurses ncurses-devel perl \
    pcre-devel wget unzip glibc glibc-devel openssl libaio-devel autoconf automake  libxml*  \
    libtool-ltdl-devel* libxml2 libxml2-devel openssl-devel libcurl-devel libjpeg-turbo* libjpeg libjpeg-devel libpng \
    libpng-devel libicu-devel openldap openldap-devel gd gd-devel php-gd  bzip2 bzip2-devel freetype \
    freetype-devel bison bison-devel zlib zlib-devel libmcrypt-devel mcrypt mhash-devel readline-devel \
    libedit-devel sqlite-devel libidn libidn-devel jq oniguruma oniguruma-devel openldap.i686 openldap-devel.i686 >>$logs_file 2>&1
    if [ "$?" != "0" ];then
        msg="基础依赖软件安装失败，退出安装！！！"
        echo $msg >>$logs_file
        echo -e "XXX\n35\n$msg\nXXX"
        echo 35
        echo "$msg" >/tmp/.php.err.info
        sleep 3
        return 1
    fi
    
    for i in glib2 glib2-devel make gcc gcc-c++ cmake curl ncurses ncurses-devel perl \
    pcre-devel wget unzip glibc glibc-devel openssl libaio-devel autoconf automake    \
     libxml2 libxml2-devel openssl-devel libcurl-devel libpng \
    libpng-devel libicu-devel openldap openldap-devel gd gd-devel php-gd  bzip2 bzip2-devel freetype \
    freetype-devel bison bison-devel zlib zlib-devel libmcrypt-devel mcrypt mhash-devel readline-devel \
    libedit-devel sqlite-devel libidn libidn-devel oniguruma oniguruma-devel
    do
        rpm -q $i >/dev/null 2>&1
        if [ "$?" != "0" ];then
            msg="基础依赖软件${i}安装失败，退出安装！！！"
            echo $msg >>$logs_file
            echo -e "XXX\n35\n$msg\nXXX"
            echo 35
            echo "$msg" >/tmp/.php.err.info
            sleep 3
            return 1
        fi
    done
    
    find $phpHome/* -type d|xargs rm -rf
    echo "注意：以下编译安装过程，仅输出错误信息，编译日志存放在/tmp/php-comp.log" >>$logs_file
    echo >/tmp/php-comp.log
    msg="安装libzip"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n40\n$msg\nXXX"
    echo 40
    echo "$msg" >>/tmp/php-comp.log
    #yum -y remove libzip >/dev/null 2>&1
    cd $phpHome
    if [ ! -f libzip-1.3.2.tar.gz ];then
        msg="下载libzip"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n45\n$msg\nXXX"
        echo 45
        curl --connect-timeout 30 -m 360 --fail -O https://libzip.org/download/libzip-1.3.2.tar.gz >>$logs_file 2>&1
    fi
    if [ "$?" != "0" ];then
        rm -rf libzip-1.3.2.tar.gz
        curl --connect-timeout 30 -m 360 --fail -O https://libzip.org/download/libzip-1.3.2.tar.gz >>$logs_file 2>&1
    fi
    tar zxf libzip-1.3.2.tar.gz>/dev/null 2>>$logs_file && \
    cd libzip-1.3.2 && ./configure >>/tmp/php-comp.log 2>>$logs_file && make >>/tmp/php-comp.log 2>>$logs_file && make install >>/tmp/php-comp.log 2>>$logs_file
    sed -i "/export PKG_CONFIG_PATH=/d" ~/.bashrc
    echo "export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/:/usr/local/lib64/pkgconfig/:/lib/pkgconfig/:/lib64/pkgconfig/"" >> ~/.bashrc
    source ~/.bashrc
    export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/:/usr/local/lib64/pkgconfig/:/lib/pkgconfig/:/lib64/pkgconfig/"
    
    msg="安装PHP"
    rm -rf $phpPath
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n50\n$msg\nXXX"
    echo 50
    echo "" >>/tmp/php-comp.log
    echo "$msg" >>/tmp/php-comp.log
    echo "PHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH">>/tmp/php-comp.log
    cd $phpHome
    tar jxf php-${phpVer}.tar.bz2 >/dev/null 2>>$logs_file
    
    
    
    if [[ "$mainPhpVer" -le "5" ]];then
        phpConfigure="./configure --prefix=$phpPath --with-config-file-path=$phpPath/etc ${AapacheSupport} --with-mysql --with-pdo-mysql --with-mysqli --with-mysql-sock --with-gd --with-iconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-bz2 --with-libxml-dir --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-pear --with-gettext --enable-session --with-mcrypt --with-mhash --enable-fpm --with-ldap"
    elif [[ "$mainPhpVer" -eq "7" ]];then
        if [[ "$subPhpVer" -le "1" ]];then
           phpConfigure="./configure --prefix=$phpPath --with-config-file-path=$phpPath/etc $AapacheSupport --with-pdo-mysql --with-mysqli --with-mysql-sock --with-gd --with-iconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-bz2 --with-libxml-dir --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-pear --with-gettext --enable-session --with-mcrypt --with-mhash --enable-fpm --with-ldap"
        elif [[ "$subPhpVer" -le "3" ]];then
           phpConfigure="./configure --prefix=$phpPath --with-config-file-path=$phpPath/etc $AapacheSupport --with-pdo-mysql --with-mysqli --with-mysql-sock --with-gd --with-iconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-bz2 --with-libxml-dir --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-ftp --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-pear --with-gettext --enable-session --with-mhash --enable-fpm --with-ldap"
        else
           phpConfigure="./configure --prefix=$phpPath --with-config-file-path=$phpPath/etc $AapacheSupport --with-pdo-mysql --with-mysqli --with-mysql-sock --enable-gd --with-iconv --with-freetype --with-jpeg --with-zlib --with-bz2 --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-ftp --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --with-zip --enable-soap --with-pear --with-gettext --enable-session --enable-fpm --with-ldap"
        fi
    else
        phpConfigure="./configure --prefix=$phpPath --with-config-file-path=$phpPath/etc $AapacheSupport --with-pdo-mysql --with-mysqli --with-mysql-sock --enable-gd --with-iconv --with-freetype --with-jpeg --with-zlib --with-bz2 --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-ftp --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --with-zip --enable-soap --with-pear --with-gettext --enable-session --enable-fpm --with-ldap"    
    fi
    echo -e "使用的编译参数为：\n$phpConfigure\n" >> $logs_file
    cd php-${phpVer} && echo -e "XXX\n55\n预编译PHP中......\nXXX" && echo 55 && $phpConfigure >>/tmp/php-comp.log 2>>$logs_file && \
    echo -e "XXX\n60\n编译PHP中......\nXXX" && echo 60 && make -j8 >>/tmp/php-comp.log 2>>$logs_file && \
    echo -e "XXX\n65\n安装PHP中......\nXXX" && echo 65 && make install >>/tmp/php-comp.log 2>>$logs_file
    if [ "$?" != "0" ];then
        msg="安装PHP失败，退出安装！！！"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n70\n$msg\nXXX"
        echo 70
        echo "$msg" >/tmp/.php.err.info
        sleep 3
        return 1
    fi
    echo " ">>$logs_file
    echo " ">>/tmp/php-comp.log
    msg="配置PHP"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n70\n$msg\nXXX"
    echo 70
    /bin/cp -f $phpPath/etc/php-fpm.conf.default $phpPath/etc/php-fpm.conf >>$logs_file 2>&1
    sed -i 's/;pid = run\/php-fpm.pid/pid = run\/php-fpm.pid/' $phpPath/etc/php-fpm.conf >>$logs_file 2>&1
    sed -i 's/;error_log = log\/php-fpm.log/error_log = log\/php-fpm.log/' $phpPath/etc/php-fpm.conf >>$logs_file 2>&1
    /bin/cp $phpPath/etc/php-fpm.d/www.conf.default $phpPath/etc/php-fpm.d/www.conf >>$logs_file 2>&1
    
    echo "设置php.ini--禁止显示PHP版本,设置时区,开启PHP短标签支持" >>$logs_file
    /bin/cp -f php.ini-production $phpPath/etc/php.ini >>$logs_file 2>&1
    sed -i 's/expose_php = On/expose_php = Off/' $phpPath/etc/php.ini >>$logs_file 2>&1
    sed -i 's/short_open_tag = Off/short_open_tag = On/' $phpPath/etc/php.ini >>$logs_file 2>&1
    sed -i 's/;date.timezone =/date.timezone = PRC/' $phpPath/etc/php.ini >>$logs_file 2>&1
  
   rm -rf /etc/php.ini
   ln -sf $phpPath/etc/php.ini /etc/php.ini  
   
    msg="启动php-fpm并设置随机启动"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n80\n$msg\nXXX"
    echo 80
    /bin/cp -f sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm >>$logs_file 2>&1
    chmod +x /etc/rc.d/init.d/php-fpm >>$logs_file 2>&1
    service php-fpm start >>$logs_file 2>&1
    /sbin/chkconfig php-fpm on >>$logs_file 2>&1
    
    echo 90
    sleep 1
    if [ -n "$apachePath" ];then
        apachePath/bin/apachectl -k graceful >/dev/null 2>&1
    fi
    echo 100
    sleep 1
        
    } | whiptail --backtitle "Author: Elvis" --title "安装PHP服务" --gauge " " 6 60 0
    if [ ! -f /tmp/php-comp.log ];then
        errInfo=`cat /tmp/.php.err.info`
        rm -rf /tmp/.php.err.info
        msg="PHP安装失败！！！"
        whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "$msg\n\n$errInfo" 10 60
        sleep 0.15
        return 1
    fi
    phpPid=`pidof php-fpm`
    if [ -n "$phpPid" ];then
        echo "PHP安装成功" >>$logs_file 
        whiptail --backtitle "Author: Elvis" --title "信息" --msgbox "PHP $phpVer 安装成功！\nPHP安装位置：$phpPath\n如果nginx/apache添加了PHP模块支持，请在重启nginx或apache后，访问http://$ipaddr/phpinfo.php检查PHP支持\n如有任何PHP扩展支持问题，请参考帮助或者自行百度" 12 60  
    else
        echo PHP $phpVer 安装失败>>$logs_file
        whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "PHP安装失败！！！" 8 60 
    fi
}




